/*******************************************************************************
 * Copyright 2017 Yuran Zhang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package baseproject.com.mybaseproject.net

import android.content.Intent
import com.bybops.app.AppConstant
import com.bybops.app.MyApplication
import com.bybops.net.BaseResponse
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

abstract class ResultObserver<T> : BaseObserver<BaseResponse<T>>() {
    abstract fun success(data: BaseResponse<T>)
    abstract fun failure(statusCode: Int, apiErrorModel: ApiErrorModel)

    // 业务状态
    private object Status {
        const val STATUS_SUCCESS = 1 // 正常
        const val STATUS_FAIL = 0 // 失败
        const val STATUS_PARAMS_LACK = 1010101 // 缺少必要参数
        const val STATUS_PARAMS_ILLEGAL = 1010102 // 参数值非法
        const val STATUS_INTERNAL_EXCEPTION = 1020101 // 接口内部异常
        const val STATUS_TOKEN_LACK = 1030101 // 缺少token
        const val STATUS_AUTH_FAIL = 1030102 // token认证失败

    }


    override fun onNext(t: BaseResponse<T>) {
        when (t.resultCode) {
            //成功
            Status.STATUS_SUCCESS -> {
                    success(t)
            }
            //token认证失败
            Status.STATUS_AUTH_FAIL ->{
                //发送广播
                var intent = Intent()
                intent.action = AppConstant.ACTION_STATUS_AUTH_FAIL
                MyApplication.instance?.sendBroadcast(intent)
            }
            //失败
            Status.STATUS_FAIL,Status.STATUS_TOKEN_LACK, Status.STATUS_PARAMS_LACK, Status.STATUS_PARAMS_ILLEGAL, Status.STATUS_INTERNAL_EXCEPTION -> {
                failure(t.resultCode, ApiErrorModel(t.resultCode, t.resultMsg))
            }
            //其他
            else -> {
                val apiErrorModel: ApiErrorModel = when (t.resultCode) {
                    ApiErrorType.INTERNAL_SERVER_ERROR.code ->
                        ApiErrorType.INTERNAL_SERVER_ERROR.getApiErrorModel()
                    ApiErrorType.BAD_GATEWAY.code ->
                        ApiErrorType.BAD_GATEWAY.getApiErrorModel()
                    ApiErrorType.NOT_FOUND.code ->
                        ApiErrorType.NOT_FOUND.getApiErrorModel()
                    else -> ApiErrorModel(t.resultCode, t.resultMsg)
                }
                failure(t.resultCode, apiErrorModel)
            }
        }
    }

    override fun onComplete() {
    }

    override fun onError(e: Throwable) {
        val apiErrorType: ApiErrorType = when (e) {
            is UnknownHostException -> ApiErrorType.NETWORK_NOT_CONNECT
            is ConnectException -> ApiErrorType.NETWORK_NOT_CONNECT
            is SocketTimeoutException -> ApiErrorType.CONNECTION_TIMEOUT
            else -> ApiErrorType.UNEXPECTED_ERROR
        }
        apiErrorType.getApiErrorModel().message = apiErrorType.getApiErrorModel().message + e.message
        failure(apiErrorType.code, apiErrorType.getApiErrorModel())
    }
}
